package com.agent.vpsserver.modules.server.controller;


import com.agent.vpsserver.common.dto.AdminPageTokenRequestDTO;
import com.agent.vpsserver.common.dto.PageBean;
import com.agent.vpsserver.common.dto.Result;
import com.agent.vpsserver.common.utils.mapping.BeansUtils;
import com.agent.vpsserver.modules.server.entity.pojo.UserAccountDO;
import com.agent.vpsserver.modules.server.manager.UserAccountManager;
import com.agent.vpsserver.modules.server.request.UserAccountSaveRequest;
import com.agent.vpsserver.modules.server.response.UserAccountPageResponse;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;

/**
 * 用户账号信息(UserAccount)表控制层
 *
 * @author guochao
 * @since 2024-03-12 23:50:24
 */
@RestController
@RequestMapping("admin/userAccount")
public class UserAccountController {
    /**
     * 服务对象
     */
    @Resource
    private UserAccountManager userAccountManager;

    @PostMapping(value = "/page")
    @ApiOperation(value = "列表")
    public Result<PageBean<UserAccountPageResponse>> page(@RequestBody @Valid AdminPageTokenRequestDTO request) {
        return Result.success(userAccountManager.pageData(request));
    }

    @PostMapping(value = "/save")
    @ApiOperation(value = "保存")
    public Result<Boolean> save(@RequestBody @Valid UserAccountSaveRequest request) {
        if (request.getMaxBandwidth() == null && request.getExpireTime() == null) {
            return Result.fail("最大流量和过期时间必填一个");
        }
        UserAccountDO userAccount = BeansUtils.map(request, UserAccountDO.class);
        userAccountManager.saveUser(userAccount);
        return Result.success(Boolean.TRUE);
    }
}
